package com.jz.community.moduleshopping.integralGoods.ui.view;

import android.app.ActionBar;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jz.community.commview.view.shadowViewHelper.ShadowProperty;
import com.jz.community.commview.view.shadowViewHelper.ShadowViewHelper;
import com.jz.community.moduleshopping.R;


/**
 * Created by Tony on 2018/7/06
 * <p>
 */
public class IntegralCouponFilterPop extends PopupWindow implements View.OnClickListener {
    private Context mContext;
    private ImageView layout;
    private TextView integral_all_tv, integral_goods_tv, integral_coupon_tv;
    private LinearLayout integral_exchange_layout;
    private IntegralHistoryListener integralHistoryListener;

    public void setIntegralHistoryListener(IntegralHistoryListener integralHistoryListener) {
        this.integralHistoryListener = integralHistoryListener;
    }

    public IntegralCouponFilterPop(Context context, ImageView layout) {
        this.mContext = context;
        this.layout = layout;
        handleShow();
    }

    private RelativeLayout view;

    private void handleShow() {
        view = (RelativeLayout) LayoutInflater.from(mContext).inflate(R.layout.module_shopping_integral_coupon_filter_item_layout, null);
        this.setContentView(view);
        this.setWidth(ActionBar.LayoutParams.MATCH_PARENT);
        this.setHeight(ActionBar.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        showAsDropDown(layout);
        initView(view);
    }

    /**
     * @param selectedType
     */
    public void handleData(int selectedType) {
        switch (selectedType) {
            case 0:
                integral_all_tv.setTextColor(ContextCompat.getColor(mContext, R.color.red));
                integral_goods_tv.setTextColor(ContextCompat.getColor(mContext, R.color.font_black));
                integral_coupon_tv.setTextColor(ContextCompat.getColor(mContext, R.color.font_black));
                break;
            case 1:
                integral_goods_tv.setTextColor(ContextCompat.getColor(mContext, R.color.red));
                integral_all_tv.setTextColor(ContextCompat.getColor(mContext, R.color.font_black));
                integral_coupon_tv.setTextColor(ContextCompat.getColor(mContext, R.color.font_black));
                break;
            case 2:
                integral_coupon_tv.setTextColor(ContextCompat.getColor(mContext, R.color.red));
                integral_goods_tv.setTextColor(ContextCompat.getColor(mContext, R.color.font_black));
                integral_all_tv.setTextColor(ContextCompat.getColor(mContext, R.color.font_black));
                break;
        }
    }

    private void initView(View view) {
        integral_all_tv = view.findViewById(R.id.integral_all_tv);
        integral_goods_tv = view.findViewById(R.id.integral_goods_tv);
        integral_coupon_tv = view.findViewById(R.id.integral_coupon_tv);
        integral_exchange_layout = view.findViewById(R.id.integral_exchange_layout);
        integral_coupon_tv.setOnClickListener(this);
        integral_goods_tv.setOnClickListener(this);
        integral_all_tv.setOnClickListener(this);
        ShadowViewHelper.bindShadowHelper(new ShadowProperty()
                .setShadowColor(0x33000000)
                .setShadowDy(2)
                .setShadowRadius(2), integral_exchange_layout);
        view.setOnClickListener(v -> {
            hideDissmiss();
        });
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.integral_all_tv) {
            handleData(0);
            integralHistoryListener.integralResult(0);
            hideDissmiss();
        }

        if (v.getId() == R.id.integral_goods_tv) {
            handleData(1);
            integralHistoryListener.integralResult(1);
            hideDissmiss();
        }

        if (v.getId() == R.id.integral_coupon_tv) {
            handleData(2);
            integralHistoryListener.integralResult(2);
            hideDissmiss();
        }

    }

    /**
     * 移除dialog
     */
    public void hideDissmiss() {
        dismiss();
    }

    public void show() {
        handleShow();
    }

    public interface IntegralHistoryListener {

        void integralResult(int type);
    }
}
